package org.stream;

import org.entity.Employee;
import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.Optional;
import java.util.OptionalDouble;

/**
 * @Description
 * @Author wjj
 * @Date 2023/4/17 22:47
 */
public class Aggregation {
    @Test
    public void aggregationTest(){
        double sum = Employee.persons().stream()
                .mapToDouble(Employee::getIncome).sum();
        System.out.println(sum);
    }

    @Test
    public void aggregationTest2(){
        Optional<Employee> max = Employee.persons().stream()
                .max(Comparator.comparingDouble(Employee::getIncome));
        String sum = max.isPresent() ? "max=" + max.get() : "not defined";
        System.out.println(sum);
    }

    @Test
    public void aggregationTest3(){
        OptionalDouble max = Employee.persons().stream()
                .mapToDouble(Employee::getIncome).max();
        String sum = max.isPresent() ? "max=" + max.getAsDouble() : "not defined";
        System.out.println(sum);
    }


}
